#include "header.h" 

int KEY;
int KEYS[2];
int KEYPRESSED;
int TIME;



void initialize(char board[][SIZE+1], int showC[][SIZE+1], int rigged, int seed){
	int i, j, k;
        
        
        // Clear the screen
        printChar('\f');

	// Format the game
	for(i =0; i<SIZE; i++)
		for(k = 0; k < SIZE; k++){
			board[i][k] = '\0';
			showC[i][k] = 1;
		}
			
	// If user selected random game, randomize the rand() seed
	if(!rigged)
		srand(TBR);
	
	
	// Populate the list
	for(k = 65; k < 65+(SIZE*SIZE)/2; k++){
		printString("Inserting ");
                printChar(k);
                printString(" @ ");
	
	
		// Insert first occurence of element
		i = rand()%SIZE;
		j = rand()%SIZE;
		
		// Make sure the random spot is empty
		// Otherwise, scan for next empty spot
		while(board[i][j] != '\0'){		
			if(++i >= SIZE){
				i = 0;
				if(++j >= SIZE)
					j = 0;
			}
		}
		
		// Insrt 1st element
		board[i][j] = k;
		printChar('(');	
                printInt(i);
                printString(", ");
                printInt(j);
                printString(") & ");
			
		// Repeat w/ second element
		i = rand()%SIZE;
		j = rand()%SIZE;
		
		// Make sure the random spot is empty
		// Otherwise, scan for next empty spot
		while(board[i][j] != '\0'){		
			if(++i >= SIZE){
				i = 0;
				if(++j >= SIZE)
					j = 0;
			}
		}
			
		// Insrt 2nd element
		board[i][j] = k;
		printString("(");	
                printInt(i);
                printString(", ");
                printInt(j);
                printString(")\n\r");	
	}


        // Show the user the board
        printBoard(board, showC);

        i = TIME;
        while(TIME < i + 5){}

        // Hide values
	for(i =0; i<SIZE; i++)
		for(k = 0; k < SIZE; k++){
			showC[i][k] = 0;
		}


        // Show the user the board
        printChar('\f');
        printBoard(board, showC);
}






void playGame(char board[][SIZE+1], int showC[][SIZE+1]){


	int stuffLeft = SIZE * SIZE;
	int i1,j1, i2, j2, i, k;
	unsigned long cnt;
	
	
	while(stuffLeft != 0){
		printString("Still ");
                printInt(stuffLeft);
                printString(" items left!\n\r");
	

                formatKeys();
		printString("\n\rEnter row, followed by column: \n\r");
                
                while(KEY != 11){
                  if(KEYPRESSED){
                    if(KEY == 10){
                      printString("Exiting Game...");
                      KEYPRESSED = 0;
                      return;
                    }
                    if(KEY > SIZE-1 && KEY != 11){
                      printString("\n\rInvalid Entry (too Large!)\n\r");
                      KEYPRESSED = 0;
                    }
                    else if(KEY != 11){
                      KEYS[0] = KEYS[1];
                      KEYS[1] = KEY;
                      printString("\r");
                      printInt(KEYS[0]);
                      printChar(' ');
                      printInt(KEYS[1]);
                      KEYPRESSED = 0;
                    }
                  }
                } 

                printString("\r\n");
                i1 = KEYS[0];
                j1 = KEYS[1];


                // Show the user the board
                showC[i1][j1] = 1;
                printBoard(board, showC);

                formatKeys();
		printString("\n\rEnter row, followed by column: \n\r");	
                
                while(KEY != 11){
                  if(KEYPRESSED){
                    if(KEY == 10){
                      printString("Exiting Game...");
                      KEYPRESSED = 0;
                      return;
                    }
                    if(KEY > SIZE-1 && KEY != 11){
                      printString("\n\rInvalid Entry (too Large!)\n\r");
                      KEYPRESSED = 0;
                    }
                    else if(KEY != 11){
                      KEYS[0] = KEYS[1];
                      KEYS[1] = KEY;
                      printChar('\r');
                      printInt(KEYS[0]);
                      printChar(' ');
                      printInt(KEYS[1]);
                      KEYPRESSED = 0;
                      
                    }
                  }
                } 


                printString("\r\n");
                i2 = KEYS[0];
                j2 = KEYS[1];


                // Show the user the board
                showC[i2][j2] = 1;
                printBoard(board, showC);

		
		if(board[i1][j1] == board[i2][j2] && (i1 != i2 || j1 != j2)){
			printString("Congratulations! You found '");
      			printChar(board[i2][j2]);
                        printString("'\n\r");
                        i = TIME;
                        while(TIME < i + 3){}
			printBoard(board, showC);
                        stuffLeft -= 2;	
		}
		else{
			printString("Wrong!\n\r");
                        // Show the user the board
			showC[i1][j1] = 0;
			showC[i2][j2] = 0;
                        i = TIME;
                        while(TIME < i + 3){}
                        printChar('\f');
                        printBoard(board, showC);  
                }
	}

        printChar('\f');
        printString("The Game lasted: ");
        printInt(TIME);
        printString(" Seconds.\n\r\n\rGood Job! The TA should give us 100%  ...hint hint!\n\r");

}




int main(void)
{
    int choice = 5;

	// Initialize board
	char board[SIZE][SIZE+1];
	int showC[SIZE][SIZE+1];

        // Initialize UART
        initUart();

        // Initialize Timer B
        initTimerB();
        // Initialize Keyboard
        KEYPRESSED = 0;
        initKeyboard();

        // Show a fancy meny
	welcomeMenu();
	
        /********************************************************************************************************/
        //TIME = time(NULL);
        /********************************************************************************************************/


	printString("Time is: ");
        printInt(TIME);
        printString("\n\r");
	// Grab a 1st TIME as soon as the menu is printed


	// Gather user input
    printString("Choice: ");
    while (choice <= 0 || choice > 2){
        if(KEYPRESSED){
          choice = KEY;
          printInt(KEY);
          if(choice <= 0 || choice >2)
            printString("\n\rInvalid Choice!\n\rChoice: ");
          KEYPRESSED = 0;
        }
	if(choice == 10){
          printString("Game OVER!!!\n\r");
		return 0;
		break;
        }	
    } 
        printString("\r\n\r\nUsing Time: ");
        printInt(TIME);
        printString(" as random seed\n\r");
        TIME = 0;

        

	// Send pointers to board elements
	// choice%2 = 1 if rigged game, 0 if random game
	// TIME = random seed based on user time reaction
	initialize(board, showC, (choice%2), TIME);

	playGame(board, showC);

	printString("Game Over!\n\r\n\r");
        printString("The Game lasted: ");
        printInt(TIME);
        printString(" Sec");
    return 0;
}

void formatKeys(void){
  KEY = -1;
  KEYS[0] = -1;
  KEYS[1] = -1;
}


void welcomeMenu(void)
{
	printString("\n\r\n\r\n\r");
	printString("           (((\n\r");
	printString("           {  '>\n\r");
	printString("           / ,)\n\r");
	printString("          /_/ /\n\r");
	printString("==========//=n===========\n\r");
	printString("         /\n\r\n\r");
	printString("  WELCOME TO BirdBrain\n\r\n\r");

        printString(" ------------------------\n\r");
	printString("| Menu: Please press        |\n\r");
	printString("|---------------------------|\n\r");
	printString("| '1' =>  New Rigged game   |\n\r");
	printString("| '2' =>  New Random Game   |\n\r");
	printString("| '*' =>  Quit              |\n\r");
	printString("|---------------------------|\n\r");

}
